package com.ts.demo;

import com.ts.domain.Student;

import java.util.ArrayList;

public class ArrayListDemo {
    public static void main(String[] args) {
        Student stu1=new Student("1001","zhangsan",20,"2010-1-1");
        Student stu2=new Student("1002","lisi",19,"2011-1-1");
        Student stu3=new Student("1003","wangwu",18,"2010-5-5");
        ArrayList<Student> list=new ArrayList<>();
        list.add(stu1);
        list.add(stu2);
        list.add(stu3);
        String id="1003";
        int index = getIndex(id, list);
        System.out.println(index);


    }
//此方法根据ID去集合list中查找索引位置，如果找到就返回索引位置，没有找到就返回-1
    public static int getIndex(String id,ArrayList<Student> list) {
        for (int i = 0; i < list.size(); i++) {
            Student stu=list.get(i);
            if(stu.getId().equals(id)){
                return i;
            }
        }
        return -1;
    }
}
